package com.sx.micowell.framework.view.chartManager

import android.graphics.Color
import android.graphics.Matrix
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.charts.HorizontalBarChart
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/8/23 18:12
 */


//fun HorizontalBarChart.test() {
//    setTestData(UI(), xAxis.UI_XAxis(), axisRight.UI_YAxis().let {
//        axisLeft.isEnabled=false
//    }).let {
//        xAxis.labelCount=data.entryCount
//    }
//}
//
//fun BarChart.test(){
//    setTestData(UI(), xAxis.UI_XAxis(), axisLeft.UI_YAxis().let {
//        axisRight.isEnabled=false
//    })
//}
//
//fun BarChart.UI() {
//    animateY(1500)//动画
//    description.isEnabled=false
//    legend.isEnabled=false
//    isDragEnabled=true
//    setScaleEnabled(false)
//}
//
//inline fun BarChart.setTestData(ui: Unit, xAxis: Unit, yAxis: Unit) {
//    ui
//    xAxis
//    yAxis
//    var arrays = ArrayList<BarEntry>()
//    for (i in 0..8) {
//        arrays.add(BarEntry(i.toFloat(), (100..500).random().toFloat()))
//    }
//    data = BarData(setNetData(arrays))
//    data.barWidth=0.6f
//    scaleX(arrays.size)
//}

//fun BarChart.setNetData(arrays: ArrayList<BarEntry>): BarDataSet {
//    var dataSet= BarDataSet(arrays, null)
//    dataSet.color=Color.parseColor("#00DA8B")//柱体颜色
//    dataSet.setDrawValues(false)//取消画值
//    return dataSet;
//}




fun BarChart.initHorizontalUIChart() {
    UIChart()
    xAxis.UI_Chart_XAxis()
    axisLeft.UI_Chart_YAxis()

}

fun BarChart.initUIChart() {
    UIBarChart()
    xAxis.UI_Bar_XAxis()
    axisLeft.UI_Bar_YAxis()
}

fun BarChart.initDataChart(arrays: List<BarEntry>, xLabile: Array<String> ?= null) {
    data = BarData(setNetData(arrays))
    xLabile?.let {
        this.xAxis.valueFormatter = IndexAxisValueFormatter(it)
        if(this !is HorizontalBarChart&&arrays.size<12){
            this.xAxis.setLabelCount(12,true)
            this.xAxis.axisMaximum=12f
        }
        data.barWidth = 0.6f
        scaleX(arrays.size)
    }
    invalidate()
}

fun BarChart.setNetData(arrays: List<BarEntry>): BarDataSet {
    var dataSet= BarDataSet(arrays, null)
    dataSet.color=Color.parseColor("#00DA8B")//柱体颜色
    dataSet.setDrawValues(false)//取消画值
    return dataSet;
}

fun BarChart.scaleX(count: Int) {
    if (count > 6) {
        val scale = count.toFloat() / 6.toFloat()
        val matrix = Matrix()
        matrix.postScale(scale, 0f)
        viewPortHandler.refresh(matrix, this, false)
        invalidate()
    }
}

